// app.js
App({
	onLaunch() {
		// 展示本地存储能力
		var res = require("./data/data.js")
		this.globalData.mallHead = res.mallHead
		this.globalData.openidkey = 'oidkey'
		this.globalData.posstorekey = 'posinfokey'
		this.globalData.openid = wx.getStorageSync(this.globalData.openidkey)
		this.globalData.posinfo = wx.getStorageSync(this.globalData.posstorekey)
		this.globalData.qrcodeurl = null
		this.globalData.orginfo = null
		this.globalData.nickavatar = null
		console.log(this.globalData.openid)
		wx.showLoading({
			title: '加载中',
		})
		this.loginCon()

	},
    loginSync() {
        return new Promise((resolve,reject)=>{
                // 登录
            wx.login({
                success (res) {
                    resolve(res)
                },
                fail(res) {
                    console.log(res)
                    wx.showToast({
                        title: '登录失败',
                        icon:'error'
                    })
                    // fail调用接口失败
                    reject({ error: '登录失败', code: 0 });
                }
            })
	})},
    async loginCon() {
		var mycode = ""
		var myid = ""
		let res = await this.loginSync()
		console.log(res)
		mycode = res.code
		if (this.getopenid()) {
			myid = this.getopenid()
		}
		var url= this.globalData.BASE_URL + '/mallLogin'
		var data = {
			code: mycode,
			uId: myid
		}
		let ret = await this.reqServer(url, data, 'GET')
		console.log(ret)
		if (ret.openId) {
			this.setopenid(ret.openId)
		}
		if (ret.avatarUrl) {
			var ndata = {
				"avatarUrl": ret.avatarUrl,
				"nickName": ret.nickName
			}
			this.setnickavatar(ndata)
		}
		wx.hideLoading()
    },
	onHide() {
		console.log(this.globalData)
		this.globalData.scene = ""
	},
	globalData: {
		mallHead: null,
		openid: null,
		nickavatar: null,
		posinfo: null,
		orginfo: null,
		qrcodeurl: null,
		BASE_URL: "https://www.jxhaha.asia/test/api/v1",
		screenHeight: 667,
		screenWidth:375,
		scene:"",
		openidkey: '',
		nickavatarkey: 'nickavatark',
		posstorekey: 'posstorek',
		offline: false,
		totalprice: 299
	},
		/**
		* px转换成rpx比例
	*/
	px2rpxRate: function () {
		let that = this;
		const rate = 750 / that.globalData.screenWidth;
		return rate;
	},
	getmallhead() {
		return this.globalData.mallHead
	},
	getopenid() {
		return this.globalData.openid;
	},
	setopenid(data) {
		this.globalData.openid = data
		wx.setStorageSync(this.globalData.openidkey, data)
		console.log(this.globalData.openid)
	},
	getnickavatar() {
		return this.globalData.nickavatar
	},
	getoffline() {
		return this.globalData.offline
	},
	setnickavatar(data) {
		this.globalData.nickavatar = data
		wx.setStorageSync(this.globalData.nickavatarkey, data)
		console.log(this.globalData.nickavatar)
	},
	setorginfo(data) {
		if (data) {
			this.globalData.orginfo = data
		}
	},
	getorginfo() {
		return this.globalData.orginfo
	},
	gettotalprice() {
		return this.globalData.totalprice
	},
	settotalprice(data) {
		this.globalData.totalprice = data
	},
	setqrurl(data) {
		if (data) {
			this.globalData.qrcodeurl = data
		}
	},
	getqrurl() {
		return this.globalData.qrcodeurl
	},
	getposinfo() {
		if (!this.globalData.posinfo) {
			this.globalData.posinfo = wx.getStorageSync(this.globalData.posstorekey)
		}
		return this.globalData.posinfo
		
	},
	setposinfo(data) {
		if (data) {
			this.globalData.posinfo = data
		}
		wx.setStorageSync(this.globalData.posstorekey, this.globalData.posinfo)
	},
	reqServer(url, data, methods) {
		return new Promise(function (resolve, reject) {
			wx.request({
				url: `${url}`,
				method: `${methods}`,
				data: data,
				header: {
					'content-type': 'application/json', // 默认值
				},
				success: function (res) {
					if (res.statusCode != 200) {
						wx.showToast({
							title: '服务器忙，请稍后再试',
							icon:'none',
							duration: 1000
						})
						reject({ error: '服务器忙，请稍后重试', code: 500 })
					}
					resolve(res.data)
				},
				fail: function (res) {
					console.log(res)
					console.log(url)
					wx.showToast({
						title: '网络无法连接',
						icon:'none',
						duration: 2000
					})
					// fail调用接口失败
					reject({ error: '网络错误', code: 0 })
				}
			})
		})
	}
})
